import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.lang.*;

public class socketJava {
 
   ServerSocket server;
   int serverPort = 3556;
   static String write=null;
   writeToFileClassHens wf = new writeToFileClassHens();
   public socketJava() {
      try 
       {
       
         server = new ServerSocket(serverPort);
         System.out.println("ServerSocket: " + server); 
       } 
       catch (IOException e) {
         e.printStackTrace();
      }
   }
 
   private void listen() {
      while (true) { // run until you terminate the program
         try {
            Socket socket = server.accept();
            //System.out.println("Socket: " + socket);
            new ClientThread(socket).start();
            
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
 
   public static void main(String[] args) {
      new socketJava().listen();
   }
 
   class ClientThread extends Thread 
   {
  int client_id;
    Socket socket;    
    String folder;
    String vehi;
    FileOutputStream fout;
    String tempTxt=null;
    StringBuilder line=new StringBuilder();
    String newString=null;
    
    
    public ClientThread(Socket socket) {
      this.socket = socket;
      //this.vehi=vehiId;
    }
 
      @Override
     public void run() {
         InputStreamReader in;
         int temp;
         try 
         {
             in = new InputStreamReader(socket.getInputStream());
            // PrintWriter outToClient = new PrintWriter(in.getOutputStream(),true);
             //outToClient.
             while(true)
             {
              temp=in.read();
              //System.out.println("ch "+temp+" "+(char)temp);
              if(temp != 0)
              {
                //System.out.println("ch "+temp+" "+(char)temp);
                line.append((char)temp);
              }
              else
              {
                //line.append(',');
              }
              
              if(temp == 41)
              {
             try
             {
                //System.out.println(line);
                newString = line.toString();
                wf.readData(newString);
                line.setLength(0);
             }
             catch (Exception e)
             {
               System.out.println("ch "+temp+" "+(char)temp);
             }
             
                
              }
             }
         } 
         catch (IOException e) 
         {
            //e.printStackTrace();
         }
   
         }
   }
}
